define(['common/js/boss-frame-angular-dev-0.1','common/js/service/boss-cart-service-0.1'],function() {
	serviceModule.factory("cookieDataStorage", function () {
		return {
			saveItem: function (key, val, options) {
				if (typeof val == "object") {
					val = JSON.stringify(val);
				}
				$.cookie(key, val, options);
			},
			getItem: function (key) {
				var val = $.cookie(key);
				try {
					val = JSON.parse(val);
				} catch (e) {
				}

				return val;
			},
			clearItem: function (key) {
				return $.removeCookie(key);
			},
		};
	});
	serviceModule.factory("dataStorage", function () {
		return {
			count: 0,
			saveItem: function (key, val) {
				if (typeof val == "object") {
					val = JSON.stringify(val);
				}

				window.sessionStorage.setItem(key, val);
				this.count++;
			},
			getItem: function (key) {
				var val = window.sessionStorage.getItem(key);
				try {
					val = JSON.parse(val);
				} catch (e) {
				}

				return val;
			},
			clearItem: function (key) {
				window.sessionStorage.removeItem(key);
				this.count--;
			},
			clearAll: function () {
				window.sessionStorage.clear();
				this.count = 0
			}
		};
	});
	serviceModule.factory("asyncDataStorageOld", function ($q) {
		return {
			count: 0,
			saveItem: function (key, val, callback) {
				var deferred = $q.defer();
				setTimeout(function(){
					if (typeof val == "object") {
						val = JSON.stringify(val);
					}
					window.sessionStorage.setItem(key, val);
					deferred.resolve("sucess");  
					}, 500);
				this.count++;
				return deferred.promise;
			},
			getItem: function (key, callback) {
				var deferred = $q.defer();
				setTimeout(function(){
					var val = window.sessionStorage.getItem(key);
					try {
						val = JSON.parse(val);
					} catch (e) {
					}
					deferred.resolve(val);  
					}, 500);
				this.count++;
				return deferred.promise;
			},
			clearItem: function (key) {
				var deferred = $q.defer();
				setTimeout(function(){
					window.sessionStorage.removeItem(key);
					this.count--;
					deferred.resolve("sucess");  
					}, 500);
				this.count++;
				return deferred.promise;
			},
			clearAll: function () {
				var deferred = $q.defer();
				setTimeout(function(){
					window.sessionStorage.clear();
					this.count = 0
					deferred.resolve("sucess");  
					}, 500);
				this.count++;
				return deferred.promise;
			}
		};
	});
	serviceModule.factory("asyncDataStorage", function ($q,CartSaleCommonService) {
		return {
			count: 0,
			saveItem: function (key, val, cartType,from,callback) {
				if(!from) {
					from = 0;
				}
				var jsonVal = JSON.stringify(val);
				var deferred = $q.defer();
				CartSaleCommonService.setGoodsToShopingCart({
					cartType : cartType ? cartType : 1,
					from : from,		
					val : jsonVal
				}).then(function(val){
					deferred.resolve(val);
				},function(msg){
					deferred.reject("购物车商品存储失败");
				})
				this.count++;
				return deferred.promise;
			},
			getItem: function (key,cartType, callback) {
				var deferred = $q.defer();
				CartSaleCommonService.getGoodsInShopingCart({
					cartType : cartType? cartType:1
				}).then(function(val){
					var obj = JSON.parse(val);
					deferred.resolve(obj);
				},function(msg){
					deferred.reject("获取购物车商品失败");
				})
				this.count++;
				return deferred.promise;
			},
			clearItem: function (key,cartType) {
				var deferred = $q.defer();
				CartSaleCommonService.clearShopingCart({
					cartType : cartType?cartType:1
				}).then(function(val){
					deferred.resolve(val);
				},function(msg){
					deferred.reject("清空购物车商品失败");
				})
				this.count++;
				return deferred.promise;
			},
			clearAll: function () {
				console.error("此功能待废弃，请用clearItem代替")
			}
		};
	});
});